//
//  JZHGiftViewController.m
//  极致荟
//
//  Created by mac on 14-4-8.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "JZHFirstLevelViewController.h"
#import "JZHDovConViewController.h"
#import "MMProgressHUD+HUDShowExtral.h"
#import "ImageCache.h"

@interface JZHFirstLevelViewController ()
{
    NSArray *dataarr;
    
}

@property (weak, nonatomic) IBOutlet UIScrollView *scoll;

@end

@implementation JZHFirstLevelViewController


- (JZHFirstLevelViewController *)initzid:(int)zid type:(int)type name:(NSString *)name
{
    JZHFirstLevelViewController *jzh=[[JZHFirstLevelViewController alloc]init];
    jzh.zid=zid;
    jzh.type=type;
    jzh.name=name;
    return jzh;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if(OSVersionIsAtLeastiOS7>=7.0)
    {
        self.automaticallyAdjustsScrollViewInsets=NO;
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self setNavigation];
    [self loadhttpdata];
}

///加载网络数据
- (void)loadhttpdata
{
//    [MMProgressHUD showWithStatus:@"数据加载中..."];
    NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithInt:self.zid],@"topicId", nil];
    [HttpRequest httpRequestPost:JZHHTTPURL(@"topic/brandList") parameters:dic block:^(id data, HttpReturnCode rerurncode) {
        if(rerurncode==HTTPReturnCodeSuccess)
        {
            self.datadic=[data objectForKey:@"data"];
            NSLog(@"%@",self.datadic);
            
            [self addscollView:self.datadic];
        }
   //     [MMProgressHUD dismiss];
    }];
}

///添加动态视图
- (void)addscollView:(NSDictionary *)dic
{
    NSArray *arr=[dic allValues];
    NSArray *idarr=[dic allKeys];
    int i=0;//数据有多少组
   
    if(arr.count%2==0)
    {
        i=arr.count/2;
    }else
    {
        i=arr.count/2+1;
    }
    [self.scoll setContentSize:CGSizeMake(0, i*200+(i*5)+5)];
    int indexselet=1;
    
       
        for (int j=0; j<i; j++)
        {
             UIView *ziview=[[UIView alloc]initWithFrame:CGRectMake(5, j*200+(j*5)+5, 310,200)];
            for(int k=0;k<2;k++)
            {
               
                UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(k*155+k*2.5, 0, ziview.frame.size.width/2-2.5, ziview.frame.size.height)];
                
                [ImageCache imageUrl:[[arr objectAtIndex:indexselet-1] objectForKey:@"image"] UiImageview:image];

                UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clike:)];
                [image setUserInteractionEnabled:YES];
                [image addGestureRecognizer:tap];
                image.tag=1000+[idarr[indexselet-1] intValue];
                [ziview addSubview:image];
                indexselet++;
                if(indexselet>arr.count)
                {
                    [self.scoll addSubview:ziview];
                    return;
                }
            }
            [self.scoll addSubview:ziview];

        }

}

- (void)clike:(UITapGestureRecognizer *)tap
{
    [self.navigationController pushViewController:[[JZHSecondViewController alloc]initZId:self.zid Sid:tap.view.tag-1000 sname:@"商品列表" type:self.type] animated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark ---导航----
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
//导航条设置
-(void)setNavigation
{
    
    //设置左按钮
    UIButton *backBut=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 12, 20)];
    [backBut setBackgroundImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
;
  
    [backBut addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [backBut.titleLabel setFont:[UIFont systemFontOfSize:14]];
    UIBarButtonItem *backBarButton=[[UIBarButtonItem alloc]initWithCustomView:backBut];
    [self.navigationItem setLeftBarButtonItem:backBarButton];
    //设置导航视图title
    self.navigationItem.title=self.name;
}
//返回
-(void)back:(id)sender
{
    [UINavigationController Pop:self];
    //[self.navigationController popViewControllerAnimated:YES];
}




@end
